package com.java.al.date;

import java.text.DateFormat;   
import java.text.ParseException;   
import java.util.Date;   
  
/**   
 * @description 計算两个日期相差的天数   
 * @author ZhouJingxian   
 *   
 */   
public class CountDate {   
  
    /**   
     * @param args   
     */   
    public static void main(String[] args) {   
        Date earlydate  = new Date();   
        Date latedate = new Date();   
        DateFormat df = DateFormat.getDateInstance();   
        try {   
            earlydate = df.parse("2011-02-18 12:00:11");   
//            latedate =  df.parse("2009-08-02 13:00");   
        } catch (ParseException e) {   
            e.printStackTrace();   
        }   
           
        int days = daysBetween(earlydate,latedate);   
           
        System.out.println(days);   
    }   
       
    /**   
     * 计算两个日期相差的天数   
     * @param early 开始时间
     * @param late  结束时间 
     * @return   
     */   
    public static final int daysBetween(Date early, Date late) {   
    	System.out.println(early);
    	System.out.println(late);
        java.util.Calendar calst = java.util.Calendar.getInstance();   
        java.util.Calendar caled = java.util.Calendar.getInstance();   
        calst.setTime(early);   
        caled.setTime(late);   
        //设置时间为0时   
        calst.set(java.util.Calendar.HOUR_OF_DAY, 0);   
        calst.set(java.util.Calendar.MINUTE, 0);   
        calst.set(java.util.Calendar.SECOND, 0);   
        caled.set(java.util.Calendar.HOUR_OF_DAY, 0);   
        caled.set(java.util.Calendar.MINUTE, 0);   
        caled.set(java.util.Calendar.SECOND, 0);   
//  System.out.println(calst.getTime().getTime());
//  System.out.println(caled.getTime().getTime());
        //得到两个日期相差的天数   
        int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24;   
        System.out.println(((caled.getTime().getTime()-calst.getTime().getTime())/1000)%86400000);
        return days;   
    }   
  
  
}  